/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include "songlistwidget.h"

#include <QWidget>
#include <QTableWidget>
#include <QHeaderView>
#include <QListWidget>
#include <QList>
#include <QIcon>

#include <QMenu>
#include <QAction>
#include <QSignalMapper>

#include <QDebug>

#include "utils.h"
#include "globals.h"

SongListWidget :: SongListWidget(QWidget* parent/*=0*/)
    :QTableWidget(parent)
{
    this->setAlternatingRowColors(true);

    this->setColumnCount(NUMBER_COLUMN+1);
    this->setHorizontalHeaderLabels(QStringList() << tr("Song id") << tr("Song") << tr("Length") << tr("Artists") << tr("Requester"));
    this->setWordWrap(false);

    QHeaderView* pHeader = new QHeaderView(Qt::Horizontal, this);
    pHeader->setResizeMode(QHeaderView::Stretch);
    pHeader->setDefaultAlignment(Qt::AlignHCenter);
    this->setHorizontalHeader(pHeader);

    // No editing
    this->setEditTriggers(QAbstractItemView::NoEditTriggers);

    // Menus
    pSongMenu = new QMenu(this);
    pSongPageSignalMapper = new QSignalMapper(this);
    pSongAction = Utils::addActionInMenu(this,pSongMenu,QIcon(),tr("See song page"),"","",pSongPageSignalMapper,SLOT(map()));

    pArtistMenu = new QMenu(this);
    pArtistPageSignalMapper = new QSignalMapper(this);
    pArtistAction = Utils::addActionInMenu(this,pArtistMenu,QIcon(),tr("See artist page"),"","",pArtistPageSignalMapper,SLOT(map()));

    pUserMenu = new QMenu(this);
    pUserPageSignalMapper = new QSignalMapper(this);
    pUserAction = Utils::addActionInMenu(this,pUserMenu,QIcon(),tr("See user page"),"","",pUserPageSignalMapper,SLOT(map()));

    connect(this, SIGNAL(itemPressed(QTableWidgetItem*)), this, SLOT(displayMenuItemClicked(QTableWidgetItem*)));

    connect(pSongPageSignalMapper, SIGNAL(mapped(const int)), this, SIGNAL(requestSongPage(const int)));
    connect(pArtistPageSignalMapper, SIGNAL(mapped(const int)), this, SIGNAL(requestArtistPage(const int)));
    connect(pUserPageSignalMapper, SIGNAL(mapped(const QString)), this, SIGNAL(requestUserPage(const QString)));

    qDebug() << "SongListWidget created";
}

SongListWidget :: ~SongListWidget()
{
    qDebug() << "SongListWidget deleted";
}

void SongListWidget :: updateInterface()
{
    unsigned int i = 0;

    qDebug() << "SongListWidget updateInterface";

    this->clearContents();
    this->setColumnHidden(0,true);

    this->setRowCount(this->data.count());

    for (QList<SongRequest>::iterator it_song = this->data.begin() ; it_song != this->data.end() ; it_song++)
    {
        QListWidget* pArtistList = new QListWidget(this);
        pArtistList->setFrameStyle(QFrame::NoFrame);

        for ( QList<Artist>::iterator it_artist = it_song->artists.begin() ; it_artist != it_song->artists.end() ; it_artist ++ )
        {
            new QListWidgetItem(QIcon(Utils::getFlagPathFromName(it_artist->flag)),it_artist->nickname,pArtistList);
        }

        QTableWidgetItem* pItemSong = new QTableWidgetItem(it_song->songInfo.title);
        QTableWidgetItem* pItemSongLength = new QTableWidgetItem(it_song->songInfo.length.toString());
        QTableWidgetItem* pItemRequester = new QTableWidgetItem(QPixmap(Utils::getFlagPathFromName(it_song->requesterInfo.flag)),it_song->requesterInfo.name);
        this->setItem(i,0,new QTableWidgetItem(QString("%1").arg(it_song->songInfo.id)));
        this->setItem(i,1,pItemSong);
        this->setItem(i,2,pItemSongLength);
        this->setCellWidget(i,3,pArtistList);
        this->setItem(i,4,pItemRequester);

        i++;

        connect(pArtistList, SIGNAL(itemPressed(QListWidgetItem*)), this, SLOT(displayMenuArtist(QListWidgetItem*)));
    }
}

void SongListWidget :: setData(const QList<SongRequest>& queueList)
{
    qDebug() << "SongListWidget setData";

    this->data = queueList;

    this->updateInterface();
}

void SongListWidget :: displayMenuItemClicked(QTableWidgetItem* pItem)
{
    qDebug() << "SongListWidget displayMenuItemClicked (" << pItem->text() << ")";

    // Disabling old messages
    pSongPageSignalMapper->removeMappings(pSongAction);
    pUserPageSignalMapper->removeMappings(pUserAction);

    switch ( pItem->column() )
    {
    case 1: // Song
        {
            int songId = this->item(pItem->row(),pItem->column()-1)->text().toUInt();
            pSongPageSignalMapper->setMapping(pSongAction,songId);
            pSongMenu->show();
            pSongMenu->move(QCursor::pos());
            // emit requestSongPage();
            break;
        }
    case 4: // User
        pUserPageSignalMapper->setMapping(pUserAction,pItem->text());
        pUserMenu->show();
        pUserMenu->move(QCursor::pos());
        // emit requestUserPage(pItem->text());
        break;
    }
}

void SongListWidget :: displayMenuArtist(QListWidgetItem* pItem)
{
    unsigned int artistId = 0;

    qDebug() << "SongListWidget displayMenuArtist (" << pItem->text() << ")";

    pArtistPageSignalMapper->removeMappings(pArtistAction);

    // Search for the artist id
    for (QList<SongRequest>::iterator it_song = this->data.begin() ; it_song != this->data.end() && !artistId ; it_song++)
    {
        for ( QList<Artist>::iterator it_artist = it_song->artists.begin() ; it_artist != it_song->artists.end() && !artistId ; it_artist ++ )
        {
            if ( it_artist->nickname == pItem->text())
            {
                artistId = it_artist->id;
            }
        }
    }

    pArtistPageSignalMapper->setMapping(pArtistAction,artistId);
    pArtistMenu->show();
    pArtistMenu->move(QCursor::pos());
}
